package com.itheima.user.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.entity.dto.BlackListDto;
import com.itheima.entity.pojo.BlackList;
import com.itheima.entity.pojo.User;
import com.itheima.entity.vo.BlackListVo;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.user.mapper.BlackListMapper;
import com.itheima.user.service.BlackListService;
import com.itheima.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author :wujunjie
 * createTime :2022-12-15 20:33
 * description :
 */
@Service
@Slf4j
public class BlackListServiceImpl implements BlackListService {
    @Autowired
    private BlackListMapper blackListMapper;

    @Autowired
    private UserService userService;

    @Override
    public Boolean insertBlackList(BlackListDto blackListDto) {
        User user = userService.selectById(blackListDto.getBlackId());
        if (ObjectUtil.isEmpty(user)){
            throw new RuntimeException("要拉黑的对象不存在");
        }
        BlackList blackList = new BlackList();
        blackList.setId(IdUtil.getSnowflakeNextIdStr());
        blackList.setUserId(ThreadLocalUtil.getId());
        blackList.setBlackId(blackListDto.getBlackId());
        blackList.setCreateTime(LocalDateTime.now());
        return blackListMapper.insertBlackList(blackList);
    }

    @Override
    public Boolean deleteBlackList(String blackId) {

        return blackListMapper.deleteBlackList(ThreadLocalUtil.getId(),blackId);
    }

    @Override
    public Object selectPage(String name, Integer page, Integer pageSize) {
        Integer start=(page-1)*pageSize;
        log.info("{}",ThreadLocalUtil.getId());
        List<BlackListVo> data =blackListMapper.selectPage(name, start, pageSize,ThreadLocalUtil.getId());
        log.info("{}",data);
        return data;
    }


}
